package com.cetc.nsfx.impala;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;



public class ImpalaJDBCUtil {
	private static ImpalaJDBCUtil impalaJDBCUtil;
	private Connection impalaConnection;
	private String user;
	private String password;
	private String driver;
	private String jdbcUrl;
	private Properties props = null;
	
/*	private static final String PASSWORD = "admin";
	private static final String USER = "admin";
	private static final String JDBC_HIVE_DRIVER = "org.apache.hive.jdbc.HiveDriver";
	// Impala的默认端口是 21050 
	private static final String hiveJDBC = "jdbc:hive2://192.168.60.98:21050/jsds_djz;auth=noSasl";*/
	/**
	 * 获取文件中的属性值
	 */
	private String getProperties(String key) {
		return props.get(key) == null ? null : (String) props.get(key);
	}
	private ImpalaJDBCUtil(){
		if(props == null){
			props = new Properties();
			//获取文件根目录路径
			InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("impalaJDBC.properties");
			try {
				props.load(inputStream);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		driver = this.getProperties("driver");
		jdbcUrl = this.getProperties("jdbcUrl");
        user = this.getProperties("user");
        password = this.getProperties("password");
		
	}
	
	//获取封装后的链接
	public static Connection getImpalaConnection() {
        if (impalaJDBCUtil == null) {
        	impalaJDBCUtil = new ImpalaJDBCUtil();
        }
        return impalaJDBCUtil.getConnection();
	}
	 
	//获取链接
	private Connection getConnection(){
		boolean conn = false;
        if (impalaConnection == null) {
            conn = true;
        }
		if(conn){
			try {
				Class.forName(driver);
				impalaConnection =  DriverManager.getConnection(jdbcUrl, user, password);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return impalaConnection;
	}
	
	/**
	 * 把返回的结果集封装成List
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	public static List<Map<String,Object>> convertList(ResultSet rs) throws SQLException {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount(); //Map rowData;
        while (rs.next()) { //rowData = new HashMap(columnCount);
            Map<String,Object> rowData = new HashMap<String,Object>();
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));
            }
            list.add(rowData);
        } return list;
    }
	/**
	 * 释放资源
	 * @param conn
	 * @param st
	 * @param rs
	 */
	public  static void close(Connection conn, Statement st,PreparedStatement pstmt,
			ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null)
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
}
